<?php
include(SERVER_ROOT_PATH.'/include/config/ml_attitude.php');
class ml_biz_addAttitude {
    
    private $result;
    static $oIlike;
    public function __construct($data){
        $this->rid=$data['rid'];
        $this->oid=$data['oid'];
        $this->atti=$data['atti'];
        $this->act_uid=$data['act_uid'];
        $this->from=$data['from'];
        $this->time=empty($data['time']) ? date('Y-m-d H:i:s', time()) : $data['time'];
        $this->oRedis =  new ml_model_rdsCount();
    }
    public function main(){
        //不在此调用checkParam
        if(!$rs=$this->addILike()){
            return $this->result;
        }
        if(!$rs=$this->addLikeMe()){
            return $this->result;
        }
        
        if(!$rs=$this->addFeed()){
            return $this->result;
        }
        $this->addCount();
        $this->addNotice();
        $this->result['code']=ML_RCODE_SUCC;
        return $this->result;
    }
    public function addILike(){
        if(!isset(self::$oIlike)){
            self::$oIlike=new ml_model_dbILike();
        }
        $rs=self::$oIlike->createLike($this->act_uid, $this->rid, $this->oid, $this->atti, $this->from, $this->time);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='add I like fail';
            return false;
        }
        return true;
    }
    public function addLikeMe(){
        $oLikeme = new ml_model_dbLikeMe();
        $rs=$oLikeme->createLike($this->oid, $this->rid, $this->act_uid, $this->atti, $this->from, $this->time);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='add like me fail';
            return false;
        }
        return true;
    }
    public function addFeed(){
        $ig = new ml_model_dbIdGenerator();
        $rs = $ig->get_userfeed_rid($this->aid, $this->feed_rid);
        if (!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='get feed_rid fail';
            return false;
        }
        $data = array();
        $data['rid'] = $this->feed_rid;
        $data['uid'] = $this->act_uid;
        $data['type'] = ML_FEED_TYPE_ATTITUDE;
        $data['content'] = '';
        $data['pic_id'] = '';
        $data['from'] = $this->from;
        $data['content_rid'] = $this->rid;
        $data['status'] = 1;
        $data['ctime'] =$this->time;
        $data['cip'] = Tool_ip::get_real_ip();
        $data['feed_info'] = array(
                                   'attitude' => $this->atti,
                                   'rid' => $this->rid,
                             );  
        $oFeed = new ml_model_dbUserfeed();
        $rs=$oFeed->insert_content($data); 
        
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='add feed fail';
            return false;
        }
        $this->oRedis->incrUserfeedcnt($this->act_uid);
        return true;
    }
    
    public function addCount(){
        $this->oRedis->incrAttitudeCount($this->rid);
        $this->oRedis->incrUserAttitudeCount($this->oid);
        
    }
    public function addNotice(){
        ml_tool_sendMq::userAction2GoodsRank($this->rid, 'like', $this->atti);
        
        $data['type']='atti';
        $data['content_rid']=$this->rid;
        $data['feed_rid']=$this->feed_rid;
        $data['uid']=$this->act_uid;
        ml_tool_sendMq::add_feed($data);        
        $oRds = new ml_model_rdsNotice();
        $oRds->add_notice(ml_tool_resid::resid2uid($this->rid), ml_model_rdsNotice::NTCTYPE_ADDATTITUDE);
    }
    public function getResult(){
        return $this->result;
    }
    public function checkAttiExist(){
        if(!isset(self::$oIlike)){
            self::$oIlike=new ml_model_dbILike();
        }
         $rs = self::$oIlike->isIAttByRid($this->act_uid,$this->rid,$isExist);
        if (!$rs) {
             $this->result['code']=ML_RCODE_BUSY;
             $this->result['msg']='check attitude exist fail';
             return false;
        }
        if ($isExist) {
              $this->result['code']=ML_RCODE_PARAM;
              $this->result['msg']='attituded yet';
              return false;
        }
        return true;
    }
    public function checkContExist(){
         $oContent = new ml_model_dbContent();
         $rs = $oContent->get_by_rid($this->rid);
         if (!$rs) {
             $this->result['code']=ML_RCODE_PARAM;
             $this->result['msg']='content by rid wrong';
             return false;
         }
         $this->_content = $rs;
         return true;
    }
    public function checkParam(){
        if(!$rs=$this->checkRid()){
            return $this->result;
        }
        if(!$rs=$this->checkUid()){
            return $this->result;
        }
        if(!$rs=$this->checkAtti()){
            return $this->result;
        }
        if(!$rs=$this->checkContExist()){
            return $this->result;
        }
        if(!$rs=$this->checkAttiExist()){
            return $this->result;
        }
        $this->result['code']=ML_RCODE_SUCC;
        return $this->result;
    }
    public function checkRid(){
        if(empty($this->rid)){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='rid empty';
            return false;
        }
        if (!ml_tool_resid::is_resid($this->rid)) {
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='rid wrong';
            return false;
        }
        return true;
    }
    public function checkAtti(){
        $attiArr=array(
            ML_ATTITUDE_OKEY,
            ML_ATTITUDE_SOSO,
            ML_ATTITUDE_BAD,
            ML_ATTITUDE_LOVE
        );
        if (!in_array($this->atti,$attiArr)) {
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='atti wrong';
            return false;
        }
        return true;
    }
    public function checkUid(){
        if(empty($this->oid)){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='oid empty';
            return false;
        }
        if (!ml_tool_isuid::is_mluid($this->oid)) {
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='oid wrong';
            return false;
        }
        if (ml_tool_resid::resid2uid($this->rid) != $this->oid){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='oid match fail';
            return false;
        }
        return true;
    }
    
}